<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class GoodsReply extends Model
{
	use \base\model\ModelTrait;
	//关联商品模型，一对多关联
	public function goodsHasMany(){
        return $this->hasMany('goods','id','goods_id');
    }
	//关联订单模型，一对一关联
	public function orderHasOne(){
        return $this->hasOne('order','id','order_id');
    }
	//关联订单商品模型，一对多关联
	public function orderGoodsHasMany(){
        return $this->hasMany('OrderGoods','order_id','order_id');
    }
	/**
     * 错误信息
     */
	public $errorInfo;
	/**
     * 验证规则
     */
	protected $rule = [
		'uid'=>'require|integer',
		'order_id'=>'require|integer',
		'goods_id'=>'require|integer',
		'comment'=>'require',
		'goods_score'=>'integer',
		'service_score'=>'integer'
	];
	
	/**
     * 获取评论列表
     * @param type $id
     * @return type
     */
	public function getList($pageIndex=1,$pageSize=20)
    {
		$model = new self;
		$model = $model->where('is_del',0);
		$model = $model->field('is_del',true);
		$model = $model->order("add_time","desc");
		$model = $model->page($pageIndex,$pageSize);
		return $model->all(null,'orderGoodsHasMany');
    }
	
	
	/**
     * 获取商品评论列表
     * @param type $id
     * @return type
     */
	public function getGoodsList($goodsId,$pageIndex=1,$pageSize=20)
    {
		$model = new self;
		$model = $model->alias('a');
		$model = $model->LeftJoin('user b',"b.id = a.uid");
		$model = $model->where('a.is_del',0);
		$model = $model->where('a.goods_id',$goodsId);
		$model = $model->field('a.*');
		$model = $model->field('b.nickname,b.sex,b.avatarUrl');
		$model = $model->order("a.add_time","desc");
		$model = $model->page($pageIndex,$pageSize);
		return $model->all();
    }
	
}
